import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;



public class KlientHanterare implements Runnable{
    
    DataInputStream streamIn;
    DataOutputStream streamOut;
    
    Thread t = new Thread(this);
    
    static TextArea txt = new TextArea(); // Visar meddelanden
    static ArrayList klienter = new ArrayList();
    
    static ResultSet rs=null;
    
    String invara="";
    String insaljare="";
    String inbudgivare="";
    String inbud="";
    
    Server s= new Server();
    
    
    
    
    
//--------------------------konstruktor------------------------------**
    public KlientHanterare(Socket SO, Server s) throws IOException{
        
        this.s = s ;
        
        streamIn = new DataInputStream(SO.getInputStream());
        streamOut = new DataOutputStream(SO.getOutputStream());
        
        t.start();
        
    }//end konstruktorn
    
//----------------------------run-----------------------------------**
    
    public void run(){
        while(true){
            try{
                
                
                String Text = streamIn.readUTF();
                
                String klocka="";
                
                int pos1=Text.indexOf("#1#");
                int pos2=Text.indexOf("#2#");
                int pos3=Text.indexOf("#3#");
                int pos4=Text.indexOf("#4#");
                int pos5=Text.indexOf("#5#");
                
                invara=(String)Text.substring(pos1+3,pos2);
                insaljare=(String)Text.substring(pos2+3,pos3);
                inbudgivare=(String)Text.substring(pos3+3,pos4);
                inbud=(String)Text.substring(pos4+3,pos5);
                klocka=(String)Text.substring(pos5+3,Text.length());
                
                String txt = "#1#" + invara + "#2#"+ insaljare + "#3#"+ inbudgivare + "#4#"+ inbud+ "#5#" + klocka;
                Klient k = new Klient();
                for(int i=0; i == klienter.size();i++){
                    
                    k=(Klient)klienter.get(i);
                    k.streamOut.writeUTF(txt);
                }//for
                
                
                //	Connect();
                /*
                                laggTillBud(Bud, Bud, Anvnamn, ForemalID, Utdatum);
                                int Budettjo=Integer.parseInt(Bud);
                                int Max=Integer.parseInt(HogstMax);
                                if(Budettjo<Max){
                 
                                        Budettjo+=10;
                                        Bud=Integer.toString(Budettjo);
                                        HogstMax=Integer.toString(Max);
                                        laggTillBud(Bud, HogstMax, HogstMaxAnv, ForemalID, Utdatum);
                 
                                        String RedigeradText="#1#"+HogstMaxAnv+"#2#"+Saljare+"#3#";
                 
                                        for(int i=0; i<Klienter.size(); i++){
                                                ((KlientHanterare)Klienter.get(i)).streamOut.writeUTF(RedigeradText+"\r\n");
                                        }//for
                 
                                }else{
                 
                                        for(int i=0; i<Klienter.size(); i++){
                                                ((KlientHanterare)Klienter.get(i)).streamOut.writeUTF(txt.getText());
                                        }//for
                                }//if-else
                 */
            }catch(IOException t){}//try-catch
        }//while
    }//end run
    
    
    public static void tick(){
        
        
        
        String dat = DBkoppling.getDate();
        dat=dat.substring(0,12);
        String dead ="";
        
        int kl= 15;
        
        //if(dat==dead){
        
        kl = kl-1;
        String klocka = String.valueOf(kl);
        
        
        Klient k = new Klient();
        
        for(int i=0; i == klienter.size();i++){
            try{
                k=(Klient)klienter.get(i);
                k.streamOut.writeUTF(klocka);
            }catch(IOException io){}
        }//for
        
        //}//else
        
    }//end tick
    
    
    
    
//----------------------------gettxtText----------------------------**
    
    
    
    
    
    
    
    
    
}